import { ElNotification } from 'element-plus'
import http from '@/http.js'

const func = {
    /**
     * 是否显示提交按钮
     */
    showSubmit: (row) => {
        if (row.state !== 0) {
            return false
        }
        if (row.createBy !== localStorage.getItem('username')) {
            return false
        }
        return true
    },

    /**
     * 是否显示撤回按钮
     */
    showRevoke: (row) => {
        if (row.allowRevoke !== 1) {
            return false
        }
        if (row.createBy !== localStorage.getItem('username')) {
            return false
        }
        return true
    },

    /**
     * 是否显示审批按钮
     */
    showApproval: (row) => {
        if (row.state === 0 || row.state === 99) {
            return false
        }
        if (row.handler == null || row.handler == '') {
            return false
        }
        if (row.handler !== localStorage.getItem('username')) {
            return false
        }
        return true
    },

    /**
     * 校验流程
     */
    validateProcess: () => {},

    /**
     * 提交流程
     */
    doSubmit: (row, callback) => {
        http.fetchJson({
            url: '/flow/task/submit',
            data: {
                flowTypeCode: row.flowTypeCode,
                taskId: row.id,
                state: row.state,
                operation: 'submit'
            }
        }).then((res) => {
            ElNotification({
                title: '提示',
                message: '提交成功',
                type: 'success'
            })
            callback()
        })
    },

    doRevoke: (row, callback) => {
        http.fetchJson({
            url: '/flow/task/revoke',
            data: {
                flowTypeCode: row.flowTypeCode,
                taskId: row.id,
                state: row.state,
                operation: 'revoke'
            }
        }).then((res) => {
            ElNotification({
                title: '提示',
                message: '撤回成功',
                type: 'success'
            })
            callback()
        })
    },

    /**
     * 流程状态格式化
     */
    stateNameFmt: (row) => {
        let state = row.state
        let allowRevoke = row.allowRevoke
        let rejectedState = row.rejectedState
        if (state === 0 && rejectedState !== 0) {
            return '被驳回'
        } else {
            return row.stateName
        }
    }
}

export default func
